package cn.sky.demo.controller;

import cn.sky.demo.domain.entity.ReminderTask;
import cn.sky.demo.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/reminder")
public class ReminderController{
    
    @Autowired
    private SmsService smsService;
    
    @PostMapping("/create")
    public ResponseEntity<String> createReminder(@RequestBody ReminderTask task) {
        try {
            // 验证开始日期不能早于今天
            if (task.getStartDate().isBefore(LocalDate.now())) {
                return ResponseEntity.badRequest().body("开始日期不能早于今天");
            }
            
            task.setIntervalDays(3);  // 固定3天间隔
            smsService.createReminderTask(task);
            return ResponseEntity.ok("提醒任务创建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("创建失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/cancel/{taskId}")
    public ResponseEntity<String> cancelReminder(@PathVariable Long taskId) {
        try {
            smsService.cancelReminder(taskId);
            return ResponseEntity.ok("提醒任务已取消");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("取消失败: " + e.getMessage());
        }
    }
}
